package com.china08.yxyapi.api.manage.edu.platform.live.lesson.end;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.live.YxyLiveCatalog;
import com.china08.yxyapi.entity.db.live.YxyLiveLesson;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.live.YxyLiveCatalogRepository;
import com.china08.yxyapi.repository.db.live.YxyLiveLessonRepository;
import com.china08.yxyapi.util.DateToolUtils;

@CrossOrigin
@RestController
public class LiveLessonEndListController extends YxyApiBaseController {

    @Autowired
    private YxyLiveLessonRepository yxyLiveLessonRepository;

    @Autowired
    private YxyLiveCatalogRepository catalogRepository;

    @RequestMapping(value = "/api_yw/manage/edu/platform/live/lesson/end/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {
        Pageable pageable = reqModel.getPageable(Direction.DESC, "createdDate");
        Page<YxyLiveLesson> page = null;
        Integer level = reqModel.level;
        String catalogId1 = reqModel.catalogId1;
        String catalogId2 = reqModel.catalogId2;
        String catalogId3 = reqModel.catalogId3;
        Integer status = reqModel.status;
        String keyword = reqModel.keyword;
        String schoolId = getProfile(request, response).getSchoolId();

        if (StringUtils.isBlank(keyword)) {
            if (level == 1) {
                if (StringUtils.isBlank(catalogId1)) {
                    if (status == 0) {
                        // 结束的
                        page = yxyLiveLessonRepository.findByCreaterUserSchoolIdAndStatusAndAuditStatus(schoolId, 2, 1,
                                pageable);
                    } else if (status == 1) {
                        // 直播并且结束
                        page = yxyLiveLessonRepository.findByCreaterUserSchoolIdAndStatusAndLiveTypeAndAuditStatus(
                                schoolId, 2, 1, 1, pageable);
                    } else if (status == 2) {
                        // 上架并且结束
                        page = yxyLiveLessonRepository.findByCreaterUserSchoolIdAndStatusAndIsVisibleAndAuditStatus(
                                schoolId, 2, 1, 1, pageable);
                    } else if (status == 3) {
                        // 下架并结束
                        page = yxyLiveLessonRepository.findByCreaterUserSchoolIdAndStatusAndIsVisibleAndAuditStatus(
                                schoolId, 2, 0, 1, pageable);
                    }

                } else {
                    if (status == 0) {
                        // 结束的
                        page = yxyLiveLessonRepository.findByCreaterUserSchoolIdAndStatusAndCatalogId1AndAuditStatus(
                                schoolId, 2, catalogId1, 1, pageable);
                    } else if (status == 1) {
                        // 直播并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndLiveTypeAndCatalogId1AndAuditStatus(schoolId, 2,
                                        1, catalogId1, 1, pageable);
                    } else if (status == 2) {
                        // 上架并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndCatalogId1AndAuditStatus(schoolId, 2,
                                        1, catalogId1, 1, pageable);
                    } else if (status == 3) {
                        // 下架并结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndCatalogId1AndAuditStatus(schoolId, 2,
                                        0, catalogId1, 1, pageable);
                    }
                }
            }
            if (level == 2) {
                if (StringUtils.isBlank(catalogId2)) {
                    if (status == 0) {
                        // 结束的
                        page = yxyLiveLessonRepository.findByCreaterUserSchoolIdAndStatusAndCatalogId1AndAuditStatus(
                                schoolId, 2, catalogId1, 1, pageable);
                    } else if (status == 1) {
                        // 直播并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndLiveTypeAndCatalogId1AndAuditStatus(schoolId, 2,
                                        1, catalogId1, 1, pageable);
                    } else if (status == 2) {
                        // 上架并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndCatalogId1AndAuditStatus(schoolId, 2,
                                        1, catalogId1, 1, pageable);
                    } else if (status == 3) {
                        // 下架并结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndCatalogId1AndAuditStatus(schoolId, 2,
                                        0, catalogId1, 1, pageable);
                    }
                } else {
                    if (status == 0) {
                        // 结束的
                        page = yxyLiveLessonRepository.findByCreaterUserSchoolIdAndStatusAndCatalogId2AndAuditStatus(
                                schoolId, 2, catalogId2, 1, pageable);
                    } else if (status == 1) {
                        // 直播并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndLiveTypeAndCatalogId2AndAuditStatus(schoolId, 2,
                                        1, catalogId2, 1, pageable);
                    } else if (status == 2) {
                        // 上架并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndCatalogId2AndAuditStatus(schoolId, 2,
                                        1, catalogId2, 1, pageable);
                    } else if (status == 3) {
                        // 下架并结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndCatalogId2AndAuditStatus(schoolId, 2,
                                        0, catalogId2, 1, pageable);
                    }
                }
            }
            if (level == 3) {
                if (StringUtils.isBlank(catalogId3)) {
                    if (status == 0) {
                        // 结束的
                        page = yxyLiveLessonRepository.findByCreaterUserSchoolIdAndStatusAndCatalogId2AndAuditStatus(
                                schoolId, 2, catalogId2, 1, pageable);
                    } else if (status == 1) {
                        // 直播并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndLiveTypeAndCatalogId2AndAuditStatus(schoolId, 2,
                                        1, catalogId2, 1, pageable);
                    } else if (status == 2) {
                        // 上架并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndCatalogId2AndAuditStatus(schoolId, 2,
                                        1, catalogId2, 1, pageable);
                    } else if (status == 3) {
                        // 下架并结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndCatalogId2AndAuditStatus(schoolId, 2,
                                        0, catalogId2, 1, pageable);
                    }
                } else {
                    if (status == 0) {
                        // 结束的
                        page = yxyLiveLessonRepository.findByCreaterUserSchoolIdAndStatusAndCatalogId3AndAuditStatus(
                                schoolId, 2, catalogId3, 1, pageable);
                    } else if (status == 1) {
                        // 直播并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndLiveTypeAndCatalogId3AndAuditStatus(schoolId, 2,
                                        1, catalogId3, 1, pageable);
                    } else if (status == 2) {
                        // 上架并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndCatalogId3AndAuditStatus(schoolId, 2,
                                        1, catalogId3, 1, pageable);
                    } else if (status == 3) {
                        // 下架并结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndCatalogId3AndAuditStatus(schoolId, 2,
                                        0, catalogId3, 1, pageable);
                    }
                }

            }
        } else {
            if (level == 1) {
                if (StringUtils.isBlank(catalogId1)) {
                    if (status == 0) {
                        // 结束的
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndNameContainingAndAuditStatus(schoolId, 2, keyword,
                                        1, pageable);
                    } else if (status == 1) {
                        // 直播并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndLiveTypeAndNameContainingAndAuditStatus(schoolId,
                                        2, 1, keyword, 1, pageable);
                    } else if (status == 2) {
                        // 上架并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndNameContainingAndAuditStatus(schoolId,
                                        2, 1, keyword, 1, pageable);
                    } else if (status == 3) {
                        // 下架并结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndNameContainingAndAuditStatus(schoolId,
                                        2, 0, keyword, 1, pageable);
                    }
                } else {
                    if (status == 0) {
                        // 结束的
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndNameContainingAndCatalogId1AndAuditStatus(
                                        schoolId, 2, keyword, catalogId1, 1, pageable);
                    } else if (status == 1) {
                        // 直播并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndLiveTypeAndNameContainingAndCatalogId1AndAuditStatus(
                                        schoolId, 2, 1, keyword, catalogId1, 1, pageable);
                    } else if (status == 2) {
                        // 上架并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndNameContainingAndCatalogId1AndAuditStatus(
                                        schoolId, 2, 1, keyword, catalogId1, 1, pageable);
                    } else if (status == 3) {
                        // 下架并结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndNameContainingAndCatalogId1AndAuditStatus(
                                        schoolId, 2, 0, keyword, catalogId1, 1, pageable);
                    }
                }
            }
            if (level == 2) {
                if (StringUtils.isBlank(catalogId2)) {
                    if (status == 0) {
                        // 结束的
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndNameContainingAndCatalogId1AndAuditStatus(
                                        schoolId, 2, keyword, catalogId1, 1, pageable);
                    } else if (status == 1) {
                        // 直播并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndLiveTypeAndNameContainingAndCatalogId1AndAuditStatus(
                                        schoolId, 2, 1, keyword, catalogId1, 1, pageable);
                    } else if (status == 2) {
                        // 上架并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndNameContainingAndCatalogId1AndAuditStatus(
                                        schoolId, 2, 1, keyword, catalogId1, 1, pageable);
                    } else if (status == 3) {
                        // 下架并结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndNameContainingAndCatalogId1AndAuditStatus(
                                        schoolId, 2, 0, keyword, catalogId1, 1, pageable);
                    }
                } else {
                    if (status == 0) {
                        // 结束的
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndNameContainingAndCatalogId2AndAuditStatus(
                                        schoolId, 2, keyword, catalogId2, 1, pageable);
                    } else if (status == 1) {
                        // 直播并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndLiveTypeAndNameContainingAndCatalogId2AndAuditStatus(
                                        schoolId, 2, 1, keyword, catalogId2, 1, pageable);
                    } else if (status == 2) {
                        // 上架并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndNameContainingAndCatalogId2AndAuditStatus(
                                        schoolId, 2, 1, keyword, catalogId2, 1, pageable);
                    } else if (status == 3) {
                        // 下架并结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndNameContainingAndCatalogId2AndAuditStatus(
                                        schoolId, 2, 0, keyword, catalogId2, 1, pageable);
                    }
                }
            }
            if (level == 3) {
                if (StringUtils.isBlank(catalogId3)) {
                    if (status == 0) {
                        // 结束的
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndNameContainingAndCatalogId2AndAuditStatus(
                                        schoolId, 2, keyword, catalogId2, 1, pageable);
                    } else if (status == 1) {
                        // 直播并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndLiveTypeAndNameContainingAndCatalogId2AndAuditStatus(
                                        schoolId, 2, 1, keyword, catalogId2, 1, pageable);
                    } else if (status == 2) {
                        // 上架并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndNameContainingAndCatalogId2AndAuditStatus(
                                        schoolId, 2, 1, keyword, catalogId2, 1, pageable);
                    } else if (status == 3) {
                        // 下架并结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndNameContainingAndCatalogId2AndAuditStatus(
                                        schoolId, 2, 0, keyword, catalogId2, 1, pageable);
                    }
                } else {
                    if (status == 0) {
                        // 结束的
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndNameContainingAndCatalogId3AndAuditStatus(
                                        schoolId, 2, keyword, catalogId3, 1, pageable);
                    } else if (status == 1) {
                        // 直播并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndLiveTypeAndNameContainingAndCatalogId3AndAuditStatus(
                                        schoolId, 2, 1, keyword, catalogId3, 1, pageable);
                    } else if (status == 2) {
                        // 上架并且结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndNameContainingAndCatalogId3AndAuditStatus(
                                        schoolId, 2, 1, keyword, catalogId3, 1, pageable);
                    } else if (status == 3) {
                        // 下架并结束
                        page = yxyLiveLessonRepository
                                .findByCreaterUserSchoolIdAndStatusAndIsVisibleAndNameContainingAndCatalogId3AndAuditStatus(
                                        schoolId, 2, 0, keyword, catalogId3, 1, pageable);
                    }
                }

            }
        }
        List<RespModel> respModels = new ArrayList<LiveLessonEndListController.RespModel>();
        if (page != null) {
            for (YxyLiveLesson yxyLiveLesson : page) {
                RespModel model = new RespModel();
                String cId1 = yxyLiveLesson.getCatalogId1();
                String cId2 = yxyLiveLesson.getCatalogId2();
                String cId3 = yxyLiveLesson.getCatalogId3();

                if (StringUtils.isNotBlank(cId1)) {
                    YxyLiveCatalog findById = catalogRepository.findById(cId1);
                    model.catalog1Id = cId1;
                    model.catalog1Name = findById.getName();
                }
                if (StringUtils.isNotBlank(cId2)) {
                    YxyLiveCatalog findById = catalogRepository.findById(cId2);
                    model.catalog2Id = cId2;
                    model.catalog2Name = findById.getName();
                }
                if (StringUtils.isNotBlank(cId3)) {
                    YxyLiveCatalog findById = catalogRepository.findById(cId3);
                    model.catalog3Id = cId3;
                    model.catalog3Name = findById.getName();
                }

                model.count = yxyLiveLesson.getCount();
                model.createDate = DateToolUtils.format(yxyLiveLesson.getCreateDate(), "yyyy-MM-dd");
                model.name = yxyLiveLesson.getName();
                model.liveType = yxyLiveLesson.getLiveType() == 0 ? "直播+录播" : "直播";
                model.schoolName = yxyLiveLesson.getTeacherSchoolNick();
                model.status = "待审核";
                model.user = yxyLiveLesson.getTeacherNick();
                model.userId = yxyLiveLesson.getTeacherId();
                model.cover = yxyLiveLesson.getCover();
                model.id = yxyLiveLesson.getId();
                model.ep = yxyLiveLesson.getEp();
                model.epTimes = yxyLiveLesson.getEpTimes();
                model.pv = yxyLiveLesson.getPv();
                model.isVisible = yxyLiveLesson.getIsVisible();
                respModels.add(model);
            }
        }
        Page<RespModel> respPage = new PageImpl<>(respModels, pageable, page.getTotalElements());

        return respPage;
    }

    public static class ReqModel extends YxyApiPageReqModel {

        public Integer level;
        public String catalogId1;
        public String catalogId2;
        public String catalogId3;
        public String keyword;
        // 0全部 1 非录播课 2 已上架 3 已下架
        public Integer status;

    }

    public static class RespModel extends YxyApiRespModel {
        public String id;
        public String createDate;
        public String name;
        public String status;
        public Integer count;
        public String cover;
        public String catalog1Name;
        public String catalog1Id;
        public String catalog2Name;
        public String catalog2Id;
        public String catalog3Name;
        public String catalog3Id;
        public String user;
        public String userId;
        public String schoolName;
        public String liveType;
        public Integer pv;// 观看次数
        public Float ep;// 评分
        public Integer epTimes;// 评分次数
        public Integer isVisible;

    }

}
